package com.dein.broadcast.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dein.broadcast.model.Broadcast;
import com.dein.broadcast.service.BroadcastService;
import com.dein.music.model.Music;

@Controller
@RequestMapping(value = "/broadcast")
public class BroadcastController {

	@Autowired
	private BroadcastService broadcastService;

	private final static String TRUE = "{result:'true'}";
	private final static String FALSE = "{result:'false'}";

	@RequestMapping(value = "/broadcastExist")
	public void broadcastExist(@RequestParam String broadcastName,
			HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		if (broadcastService.broadcastExist(broadcastName)) {
			out.print(TRUE);
		} else {
			out.print(FALSE);
		}
	}

	@RequestMapping(value = "/createBroadcast")
	public void createBroadcast(@RequestParam String broadcastName,
			@RequestParam long[] musicIds, HttpServletResponse response)
			throws Exception {
		System.out.println(musicIds[0]);
		PrintWriter out = response.getWriter();
		if (broadcastService.createBroadcast(broadcastName, musicIds)) {
			out.print(TRUE);
		} else {
			out.print(FALSE);
		}
	}

	@RequestMapping(value = "/getBroadcastList")
	public String getBroadcastList(HttpServletRequest request) {
		request.setAttribute("broadcastList",
				broadcastService.getBroadcastList());
		return "admin_broadcast";
	}

	@RequestMapping(value = "/getBroadcastMusicList")
	public String getBroadcastMusicList(
			@RequestParam(value = "id") int broadcastId,
			@RequestParam(value = "name") String broadcastName,
			HttpServletRequest request) {
		Broadcast broadcast = new Broadcast();
		broadcast.setBroadcastId(broadcastId);
		try {
			broadcast.setBroadcastName(new String(broadcastName
					.getBytes("ISO-8859-1"), "utf-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		broadcast.setBroadcastMusicList(broadcastService
				.getBroadcastMusicList(broadcastId));
		request.setAttribute("broadcast", broadcast);
		return "admin_broadcastMusicList";
	}

	@RequestMapping(value = "/updateBroadcast")
	public void updateBroadcast(@RequestParam int broadcastId,
			@RequestParam String broadcastName, HttpServletResponse response)
			throws IOException {
		PrintWriter out = response.getWriter();
		if (broadcastService.updateBroadcast(broadcastId, broadcastName)) {
			out.print(TRUE);
		} else {
			out.print(FALSE);
		}
	}

	@RequestMapping(value = "/deleteBroadcastMusic")
	public void deleteBroadcastMusic(@RequestParam int broadcastId,
			@RequestParam long musicId, HttpServletResponse response)
			throws IOException {
		PrintWriter out = response.getWriter();
		if (broadcastService.deleteBroadcastMusic(broadcastId, musicId)) {
			out.print(TRUE);
		} else {
			out.print(FALSE);
		}
	}

	@RequestMapping(value = "/addBroadcastMusic")
	public void addBroadcastMusic(@RequestParam int broadcastId,
			@RequestParam long musicId, HttpServletResponse response)
			throws IOException {
		PrintWriter out = response.getWriter();
		if (broadcastService.addBroadcastMusic(broadcastId, musicId)) {
			out.print(TRUE);
		} else {
			out.print(FALSE);
		}
	}

	@RequestMapping(value = "/deleteBroadcast")
	public void deleteBroadcast(@RequestParam int broadcastId,
			HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		if (broadcastService.deleteBroadcast(broadcastId)) {
			out.print(TRUE);
		} else {
			out.print(FALSE);
		}
	}

	@RequestMapping(value = "/playPublicBroadcast")
	@ResponseBody
	public List<Music> playPublicBroadcast(@RequestParam int broadcastId) {
		return broadcastService.playPublicBroadcast(broadcastId);
	}

	@RequestMapping(value = "/playRedHeartBroadcast")
	@ResponseBody
	public List<Music> playRedHeartBroadcast(HttpServletRequest request) {
		long userId = (Long) request.getSession().getAttribute("userId");
		return broadcastService.playRedHeartBroadcast(userId);
	}

	@RequestMapping(value = "/playPrivateBroadcast")
	@ResponseBody
	public List<Music> playPrivateBroadcast(HttpServletRequest request) {
		long userId = (Long) request.getSession().getAttribute("userId");
		return broadcastService.playPrivateBroadcast(userId);
	}

}
